package servlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletException;
import java.io.IOException;

import bean.Log;
import bean.User;
import dao.LogManagerDao;
import dao.LogManagerDaoImpl;
import dao.UserManagerDaoImpl;

public class alterUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
		request.setCharacterEncoding("UTF-8");
		String id = request.getParameter("userid") ;
		String name = request.getParameter("username");
		String pwd = request.getParameter("password");
		String auth = request.getParameter("authority");
		int admin = 0;
		if(auth.equals("user")) {
			admin=0;
		}else {
			admin=1;
		}
		
		User user = new User();
		UserManagerDaoImpl userdao = new  UserManagerDaoImpl();
		LogManagerDao logdao =new LogManagerDaoImpl();
		Log log=new Log();
		HttpSession session = request.getSession();
		User user1=(User) session.getAttribute("user");
		user.setUser_id(id);
		//user.setName(name);
		if(pwd.equals("")) {
			user.setPassword(userdao.getUser(id).get(0).getPassword());
			pwd = userdao.getUser(id).get(0).getPassword();
		}else {
			user.setPassword(pwd);
		}
		user.setAdmin(admin);
		
		if(userdao.updateUser(user, pwd, admin)){
			log.setExcution("修改用户");
			log.setExcutor(user1.getUser_id());
			log.setState(true);
			logdao.addLog(log);
		}
		else{
			log.setExcution("修改用户");
			log.setExcutor(user1.getUser_id());
			log.setState(false);
			logdao.addLog(log);
		}
		
		request.getRequestDispatcher("userManage.jsp").forward(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}
